import time

#  文本
from PyQt5.QtSql import QSqlQuery

MEDIA_TYPE_TEXT = 1
# 文件
MEDIA_TYPE_FILE = 10
# 图片
MEDIA_TYPE_IMG = 4
# 表情
MEDIA_TYPE_EMOJI = 6
# 撤回
MEDIA_TYPE_WITHDRAW = 11
# MediaTypeNews = 2 // 新闻
# MediaTypeVoice = 3 // 语音
# MediaTypeRedEnvelope = 5 // 红包
# MediaTypeLink = 7 // 链接
# MediaTypeVideo = 8 // 视频
# MediaTypeContact = 9 // 通讯录


# 消息类型定义
CMD_SINGLE_MSG = 10
CMD_ACK = 1
CMD_UNDELIVERED = 12
CMD_GROUP_MSG = 11
CMD_HEART = 2
CMD_NOT_IN_GROUP = 3


class FznChatMessage:
    """
    服务器端的消息定义，
    type Message struct {
    Id      string `json:"id,omitempty" form:"id"`           //消息ID
    UserId  int64  `json:"userId,omitempty" form:"userId"`   //谁发的消息
    Cmd     int    `json:"cmd,omitempty" form:"cmd"`         //消息的类型：群聊、私聊，具体的值参照前面的常量
    DstId   int64  `json:"dstId,omitempty" form:"dstId"`     //目的端用户ID、群ID
    GroupId int64  `json:"groupId,omitempty" form:"groupId"` //群消息时表明所在群的ID
    Media   int    `json:"media,omitempty" form:"media"`     //消息按照什么样式展示：文本、图片，具体值参见前面的常量
    Content string `json:"content,omitempty" form:"content"` //消息的内容
    Pic     string `json:"pic,omitempty" form:"pic"`         //预览图片，也就是缩略图，用在图片、视频的预显示上，实际内容在Url中
    Url     string `json:"url,omitempty" form:"url"`         //服务的URL,用在图片、表情、附件消息中
    Memo    string `json:"memo,omitempty" form:"memo"`       //简单描述，例如红包的描述
    Amount  int    `json:"amount,omitempty" form:"amount"`   //其他和数字相关的，例如红包的金额，语音的长度
    Created string `json:"created,omitempty" form:"created"` //客户端毫秒级时间戳
    }
    """

    def __init__(self, user_id, dst_id=None, cmd=None, message_id=None):
        # 消息从哪个用户发出的
        # 消息的目的地，可以是用户，也可以是群
        self.userId = int(user_id)

        # 根据目标ID的值，设置单聊的ID或者是群聊的ID
        self.dstId = None
        self.groupId = None
        if dst_id:
            dst_id = int(dst_id)
            if dst_id < 1000000:
                self.dstId = dst_id
            else:
                self.groupId = dst_id

        # 消息初始的时候设置为文本消息
        self.media = MEDIA_TYPE_TEXT
        self.content = None

        # 设置消息类型，默认为单聊消息
        if cmd is not None:
            self.cmd = cmd
        elif dst_id < 1000000:
            self.cmd = CMD_SINGLE_MSG
        else:
            self.cmd = CMD_GROUP_MSG

        current_time = time.time()
        local_time = time.localtime(current_time)
        # 消息的创建时间
        self.created = time.strftime("%Y-%m-%d %H:%M:%S", local_time)

        # 消息ID
        if message_id:
            self.id = message_id
        else:
            millis = (current_time - int(current_time)) * 1000
            date_str = time.strftime("%Y%m%d%H%M%S", local_time)
            self.id = "%s.%s.%03d" % (user_id, date_str, millis)

        self.pic = None
        self.url = None
        self.memo = None
        self.amount = None

    def set_content(self, content: str) -> None:
        self.content = content

    def set_message_id(self, message_id: str) -> None:
        if message_id:
            self.id = message_id

    def set_media(self, media: int) -> None:
        self.media = media

    def set_url(self, url: str) -> None:
        self.url = url

    def set_cmd(self, cmd: int) -> None:
        self.cmd = cmd

    def set_memo(self, memo: str):
        self.memo = memo

    def set_amount(self, amount: int):
        self.amount = amount

    def set_pic(self, pic: str):
        self.pic = pic

    def set_created(self, created: str):
        self.created = created

    def set_group_id(self, group_id: int):
        self.groupId = group_id

    def set_dst_id(self, dst_id: int):
        self.dstId = dst_id

    def __getitem__(self, key):
        return self.__dict__.get(key, "")

    def from_query_result(self, query: QSqlQuery) -> None:
        """
        通过数据库的查询结果来设置各个对象。
        :param query:
        :return:
        """
        self.dstId = query.value(2)
        self.groupId = query.value(3)
        self.media = query.value(5)
        self.content = query.value(6)
        self.pic = query.value(7)
        self.url = query.value(8)
        self.memo = query.value(9)
        self.amount = query.value(10)
        self.created = query.value(11)
